package com.zbs.framework.common.enums;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.google.common.base.Throwables;

import java.io.IOException;

/**
 * 返回   "enum": {"code": "2","message": "tom2"} 格式给前端
 */
 
public class JsonEnumSerializer extends JsonSerializer<BaseEnum> {
    @Override
    public void serialize(BaseEnum value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        try {
            //类的code值
            Object code = value.getCode();
            //这里也不用反射了,性能不好,所以直接new一个map再做序列化
            serializers.defaultSerializeValue(code, gen);
        } catch (Exception e) {
            System.out.println("JsonEnumSerializer serialize error: " + Throwables.getStackTraceAsString(e));
            throw new RuntimeException(e);
        }
    }
}